package com.process;

import com.dto.User;

import java.lang.reflect.Field;

/**
 * @author 惊云
 * @date 2023/8/20 8:35
 */
public class SqlParse {

    private String SELECT_SQL = "SELECT * FROM {table} ";
    private String DELETE_SQL = "DELETE FROM {table} ";
    private String INSERT_SQL = "INSERT INTO {table} (XXX) values (YYY) ";
    private String UPDATE_SQL = "UPDATE {table} SET ";

    public <T> String getSelectSql(T t,String table) throws IllegalAccessException {
        // SELECT * FROM table WHERE a=x AND b = x;
        Class<?> aClass = t.getClass();
        Field[] fields = aClass.getDeclaredFields();
        String WHERE = "";
        for (Field field : fields) {
            field.setAccessible(true);
            Object o = field.get(t);
            String name = field.getName();
            if(o != null){
                WHERE =  WHERE + " AND " + name + " = " + o.toString();
            }
        }

        if(WHERE != ""){
            WHERE = "WHERE  " + WHERE;
        }
        SELECT_SQL = SELECT_SQL.replace("{table}",table);
        return SELECT_SQL + getWhereSql(t,table);

    }

    public <T> String getDeleteSQL(T t,String table) throws IllegalAccessException {
        DELETE_SQL = DELETE_SQL.replace("{table}",table);
        return DELETE_SQL + getWhereSql(t,table);
    }

    public <T> String getUpdateSQL(T before,T after,String table) throws IllegalAccessException {
        String update = getWhereSql(after, table).replace("WHERE"," ").replace("AND",", ");
        String WHERE_SQL = getWhereSql(before, table);

        return UPDATE_SQL.replace("{table}",table) +" "+ update+ " "+ WHERE_SQL;

    }

    public <T> String getInsert(T t,String table) throws IllegalAccessException {
        // insert into user(a,b) values (a,b);
        String fieldString = "";
        String valueString = "";
        Class<?> aClass = t.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Object o = field.get(t);
            String name = field.getName();
            if(o != null){
                fieldString = fieldString + "," + name;
                if(field.getType() == String.class){
                    valueString = valueString + ",'" + o.toString()+"' ";
                }else {
                    valueString = valueString + "," + o.toString();
                }

            }
        }

        String s = fieldString.replaceFirst(",", "");
        String ss = valueString.replaceFirst(",", "");

        String insertSQL = INSERT_SQL.replace("{table}", table).replace("XXX", s).replace("YYY", ss);

        return insertSQL;
    }



    public <T> String getWhereSql(T t,String table) throws IllegalAccessException {
        // SELECT * FROM table WHERE a=x AND b = x;
        Class<?> aClass = t.getClass();
        Field[] fields = aClass.getDeclaredFields();
        String WHERE = "";
        for (Field field : fields) {
            field.setAccessible(true);
            Object o = field.get(t);
            String name = field.getName();
            if(o != null){
                if(field.getType() == String.class){
                    WHERE =  WHERE + " AND " + name + " = '" + o.toString() +"' ";
                }else {
                    WHERE =  WHERE + " AND " + name + " = " + o.toString();
                }

            }
        }

        if(WHERE != ""){
            WHERE = "WHERE  " + WHERE;
        }
        return  WHERE.replaceFirst("AND"," ");

    }


    public static void main(String[] args) throws IllegalAccessException {
        SqlParse sqlParse = new SqlParse();
        User user = new User();
        user.setId(15);
        user.setAge(1);
        user.setPhone("222");
        String selectSql = sqlParse.getSelectSql(user, "user");
//        System.out.println(selectSql);

        String insert = sqlParse.getInsert(user, "user");
        System.out.println(insert);

        String update = sqlParse.getUpdateSQL(user, user, "user");
        System.out.println(update);



    }
}
